package br.com.toscano.gobra.view;

import java.io.ByteArrayInputStream;

import javax.faces.context.FacesContext;
import javax.inject.Inject;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import br.com.toscano.gobra.business.ConfiguracaoBC;
import br.com.toscano.gobra.domain.Configuracao;
import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.stereotype.ViewController;
import br.gov.frameworkdemoiselle.template.AbstractEditPageBean;
import br.gov.frameworkdemoiselle.transaction.Transactional;

@ViewController
@PreviousView("./configuracao_list.jsf")
public class ConfiguracaoEditMB extends
		AbstractEditPageBean<Configuracao, Long> {

	private static final long serialVersionUID = 1L;

	@Inject
	private ConfiguracaoBC configuracaoBC;

	@Override
	@Transactional
	public String delete() {
		this.configuracaoBC.delete(getId());
		return getPreviousView();
	}

	private StreamedContent imagem;

	public StreamedContent getImagem() {
		if (this.getBean() != null && this.getBean().getImagem() != null && this.getBean().getImagem().length > 0) {
			try {
				imagem = new DefaultStreamedContent(new ByteArrayInputStream(this.getBean().getImagem()), this.getBean().getUrlImagemMunicipio(), this.getBean().getNomeImagem());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return imagem;
	}

	public void montaObjeto() {
		
		this.getBean().setImagem((byte[])FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("logoPrefeitura"));
		this.getBean().setUrlImagemMunicipio((String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("contentType"));
		this.getBean().setNomeImagem((String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("nomeArquivo"));

	}

	@Override
	@Transactional
	public String insert() {
		montaObjeto();
		this.configuracaoBC.insert(getBean());
		return getPreviousView();
	}

	@Override
	@Transactional
	public String update() {
		montaObjeto();
		this.configuracaoBC.update(getBean());
		return getPreviousView();
	}

	@Override
	protected Configuracao handleLoad(Long id) {
		setBean(this.configuracaoBC.load(id));

		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("logoPrefeitura", this.getBean().getImagem());
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("contentType", this.getBean().getUrlImagemMunicipio());
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("nomeArquivo", this.getBean().getNomeImagem());

		return getBean();
	}

	public void handleFileUpload(FileUploadEvent event) {
		this.getBean().setImagem(event.getFile().getContents());
		this.getBean().setNomeImagem(event.getFile().getFileName());
		this.getBean().setUrlImagemMunicipio(event.getFile().getContentType());
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("logoPrefeitura", event.getFile().getContents());
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("contentType", event.getFile().getContentType());
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("nomeArquivo", event.getFile().getFileName());
	}
}